package com.serkol.ams.controller.api;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.SysAgentPermission;
import com.serkol.ams.entity.SysRole;
import com.serkol.ams.entity.SysRoleNavPermission;
import com.serkol.ams.entity.SysRolePermission;
import com.serkol.ams.service.*;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("api/permission")
public class PermissionApiController {
    @Autowired
    private NavigationService navigationService;
    @Value("${upload.host}")
    private String uploadhost;

    @Autowired
    private SysAgentPermissionService sysAgentPermissionService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysRoleNavPermissionService sysRoleNavPermissionService;

    @Autowired
    private SysRoleService sysRoleService;


    @PostMapping("/agentDistributionNav")
    @ResponseBody
    public Result agentDistributionNav(@RequestBody JSONObject data) {
        System.out.println(data);
        long agentId = data.getLongValue("agentId");
        JSONArray ids = data.getJSONArray("ids");
        List<Long> longs = ids.toJavaList(Long.class);
        QueryWrapper<SysAgentPermission> deleteByQuery = new QueryWrapper<>();
        deleteByQuery.lambda().eq(SysAgentPermission::getAgentId, agentId).notIn(SysAgentPermission::getCategoryId, longs);
        sysAgentPermissionService.getBaseMapper().delete(deleteByQuery);
        Iterator<Long> iterator = longs.iterator();
        List<SysAgentPermission> inserLog = new ArrayList<>();
        while (iterator.hasNext()) {

            Long next = iterator.next();
            SysAgentPermission agentPermission = new SysAgentPermission();
            agentPermission.setAgentId(agentId);
            agentPermission.setCategoryId(next);
            inserLog.add(agentPermission);
        }
        boolean b = sysAgentPermissionService.saveBatch(inserLog);


        return b ? Result.ok() : Result.failure();
    }


    @GetMapping("/agentDistributionList")
    @ResponseBody
    public Result agentDistributionList(@RequestParam(value = "id", defaultValue = "") long id) {

        LambdaQueryWrapper<SysAgentPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysAgentPermission::getCategoryId);
        queryWrapper.eq(SysAgentPermission::getAgentId, id);
        List<Object> objects = sysAgentPermissionService.listObjs(queryWrapper);

        return Result.ok(objects);
    }




    @PostMapping("/roleNavDistributionNav")
    @ResponseBody
    public Result roleNavDistributionNav(@RequestBody JSONObject data) {
        System.out.println(data);
        long roleId = data.getLongValue("roleId");
        JSONArray ids = data.getJSONArray("ids");
        List<Long> longs = ids.toJavaList(Long.class);
        QueryWrapper<SysRoleNavPermission> deleteByQuery = new QueryWrapper<>();
        deleteByQuery.lambda().eq(SysRoleNavPermission::getRoleId, roleId).notIn(SysRoleNavPermission::getCategoryId, longs);
        sysRoleNavPermissionService.getBaseMapper().delete(deleteByQuery);
        Iterator<Long> iterator = longs.iterator();
        List<SysRoleNavPermission> inserLog = new ArrayList<>();
        while (iterator.hasNext()) {

            Long next = iterator.next();
            SysRoleNavPermission navPermission = new SysRoleNavPermission();
            navPermission.setRoleId(roleId);
            navPermission.setCategoryId(next);
            inserLog.add(navPermission);
        }
        boolean b = sysRoleNavPermissionService.saveBatch(inserLog);


        return b ? Result.ok() : Result.failure();
    }


    @GetMapping("/roleNavDistributionList")
    @ResponseBody
    public Result roleNavDistributionList(@RequestParam(value = "id", defaultValue = "") long id) {

        LambdaQueryWrapper<SysRoleNavPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysRoleNavPermission::getCategoryId);
        queryWrapper.eq(SysRoleNavPermission::getRoleId, id);
        List<Object> objects = sysRoleNavPermissionService.listObjs(queryWrapper);

        return Result.ok(objects);
    }





    @PostMapping("/roleDistributionNav")
    @ResponseBody
    public Result roleDistributionNav(@RequestBody JSONObject data) {
        System.out.println(data);
        long roleId = data.getLongValue("roleId");
        JSONArray ids = data.getJSONArray("ids");
        List<Long> longs = ids.toJavaList(Long.class);
        QueryWrapper<SysRolePermission> deleteByQuery = new QueryWrapper<>();
        deleteByQuery.lambda().eq(SysRolePermission::getRoleId, roleId).notIn(SysRolePermission::getCategoryId, longs);
        sysRolePermissionService.getBaseMapper().delete(deleteByQuery);
        Iterator<Long> iterator = longs.iterator();
        List<SysRolePermission> inserLog = new ArrayList<>();
        while (iterator.hasNext()) {

            Long next = iterator.next();
            SysRolePermission agentPermission = new SysRolePermission();
            agentPermission.setRoleId(roleId);
            agentPermission.setCategoryId(next);
            inserLog.add(agentPermission);
        }
        boolean b = sysRolePermissionService.saveBatch(inserLog);
        return b ? Result.ok() : Result.failure();
    }


    @GetMapping("/roleDistributionList")
    @ResponseBody
    public Result roleDistributionList(@RequestParam(value = "id", defaultValue = "") long id) {

        LambdaQueryWrapper<SysRolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysRolePermission::getCategoryId);
        queryWrapper.eq(SysRolePermission::getRoleId, id);
        List<Object> objects = sysRolePermissionService.listObjs(queryWrapper);
        return Result.ok(objects);
    }
    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
      //  Result result = new Result();

        return sysRoleService.findByList(pageSize, current);
    }

    @IsLogin
    @RequestMapping("edit")
    public Result edit(@RequestBody SysRole sysRole) {

        boolean b = false;
        if (sysRole.getId() != null && sysRole.getId() > 0) {
            b = sysRoleService.updateById(sysRole);
        } else {
            b = sysRoleService.save(sysRole);
        }
        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }
}
